﻿using RotovapPlatform.Domain.Business.Storage.Entities;
using RotovapPlatform.Domain.Business.Storage.Entities.Grippers;
using RotovapPlatform.Domain.Business.Storage.Enums;
using RotovapPlatform.Infrastructure.Common.Data;
using static RotovapPlatform.Domain.Business.Machine.Enums.ExperimentConfigEnums;

public static class StorageLocationSeed
{
	public static async Task SeedAsync(StorageDbContext context)
	{
		if (!context.StorageLocations.Any())
		{
			var locations = new List<StorageLocation>();

			// 创建各种测试用的瓶子

			// 1. 旋蒸瓶系列
			// 1.1 空旋蒸瓶1000ml
			var rotovapBottle1000Empty = new RotovapBottle()
			{
				Function = FunctionType.Rotovap_Empty,
				MaterialName = "空旋蒸瓶1000ml",
				IsClean = true,
				ExperimentId = null,
				StackInTime = DateTime.Now.AddDays(-5),
				Operator = "张工",
				BarCode = "RV1000-001"
			};
			rotovapBottle1000Empty.SetAs1000ml();

			// 1.2 非空旋蒸瓶1000ml（残液）
			var rotovapBottle1000Residue = new RotovapBottle()
			{
				Function = FunctionType.Rotovap_NotEmpty,
				MaterialName = "乙醇残液1000ml",
				IsClean = false,
				ExperimentId = Guid.NewGuid(),
				ExperimentName = "乙醇浓缩实验",
				ExperimentDescription = "使用旋转蒸发仪浓缩乙醇溶液",
				ExperimentStartTime = DateTime.Now.AddHours(-5),
				ExperimentEndTime = DateTime.Now.AddHours(-1),
				StackInTime = DateTime.Now.AddHours(-1),
				Operator = "张工",
				BarCode = "RV1000-002"
			};
			rotovapBottle1000Residue.SetAs1000ml();

			// 1.3 空旋蒸瓶500ml
			var rotovapBottle500Empty = new RotovapBottle()
			{
				Function = FunctionType.Rotovap_Empty,
				MaterialName = "空旋蒸瓶500ml",
				IsClean = true,
				ExperimentId = null,
				StackInTime = DateTime.Now.AddDays(-3),
				Operator = "李工",
				BarCode = "RV500-001"
			};

			// 1.4 非空旋蒸瓶500ml（残液）
			var rotovapBottle500Residue = new RotovapBottle()
			{
				Function = FunctionType.Rotovap_NotEmpty,
				MaterialName = "丙酮残液500ml",
				IsClean = false,
				ExperimentId = Guid.NewGuid(),
				ExperimentName = "丙酮浓缩实验",
				ExperimentDescription = "使用旋转蒸发仪浓缩丙酮溶液",
				ExperimentStartTime = DateTime.Now.AddHours(-8),
				ExperimentEndTime = DateTime.Now.AddHours(-6),
				StackInTime = DateTime.Now.AddHours(-6),
				Operator = "李工",
				BarCode = "RV500-002"
			};

			// 2. 离心瓶系列
			// 2.1 空离心瓶
			var centrifugeBottleEmpty = new CentrifugeBottle()
			{
				Function = FunctionType.Centrifuge_Empty,
				MaterialName = "空离心瓶",
				CurrentVolume = 0,
				ExperimentId = null,
				StackInTime = DateTime.Now.AddDays(-2),
				Operator = "王工",
				BarCode = "CF500-001"
			};

			// 2.2 待旋蒸原料离心瓶
			var centrifugeBottleToRotovap = new CentrifugeBottle()
			{
				Function = FunctionType.Centrifuge_To_Rotovap,
				MaterialName = "乙醇溶液",
				OilBathTemperature = 60,
				InitialVolume = 450,
				CurrentVolume = 450,
				ExperimentId = Guid.NewGuid(),
				ExperimentName = "乙醇浓缩实验",
				ExperimentDescription = "使用旋转蒸发仪浓缩乙醇溶液",
				ExperimentStartTime = DateTime.Now.AddHours(-2),
				StackInTime = DateTime.Now.AddHours(-2),
				Operator = "张工",
				BarCode = "CF500-002",
				CollectionCompleteTime = DateTime.Now.AddHours(-2)
			};

			// 2.3 冷凝液收集瓶
			var centrifugeBottleCondensate = new CentrifugeBottle()
			{
				Function = FunctionType.Centrifuge_Condensate_Collection,
				MaterialName = "乙醇冷凝液",
				CurrentVolume = 350,
				ExperimentId = Guid.NewGuid(),
				ExperimentName = "乙醇浓缩实验",
				ExperimentDescription = "乙醇浓缩实验的冷凝液收集",
				ExperimentStartTime = DateTime.Now.AddHours(-3),
				StackInTime = DateTime.Now.AddHours(-3),
				Operator = "张工",
				BarCode = "CF500-003",
				CollectionCompleteTime = DateTime.Now.AddHours(-1)
			};

			// 2.4 旋蒸残液收集瓶
			var centrifugeBottleResidue = new CentrifugeBottle()
			{
				Function = FunctionType.Centrifuge_Residue_Collection,
				MaterialName = "丙酮残液",
				CurrentVolume = 100,
				ExperimentId = Guid.NewGuid(),
				ExperimentName = "丙酮浓缩实验",
				ExperimentDescription = "丙酮浓缩实验的残液收集",
				ExperimentStartTime = DateTime.Now.AddHours(-6),
				StackInTime = DateTime.Now.AddHours(-2),
				Operator = "李工",
				BarCode = "CF500-004",
				CollectionCompleteTime = DateTime.Now.AddHours(-2)
			};

			// 2.5 洗液离心瓶
			var centrifugeBottleWash = new CentrifugeBottle()
			{
				Function = FunctionType.Centrifuge_Wash_Liquid,
				MaterialName = "清洗液",
				CurrentVolume = 400,
				ExperimentId = Guid.NewGuid(),
				ExperimentName = "设备清洗",
				ExperimentDescription = "旋蒸设备清洗",
				ExperimentStartTime = DateTime.Now.AddHours(-1),
				StackInTime = DateTime.Now.AddHours(-1),
				Operator = "王工",
				BarCode = "CF500-005",
				CollectionCompleteTime = DateTime.Now.AddHours(-1)
			};

			// 3. 取样瓶系列
			// 3.1 空取样瓶
			var sampleBottleEmpty = new SampleBottle()
			{
				Function = FunctionType.Sample_Empty,
				MaterialName = "空取样瓶",
				CurrentVolume = 0,
				ExperimentId = null,
				StackInTime = DateTime.Now.AddDays(-1),
				Operator = "李工",
				BarCode = "SP008-001"
			};

			// 3.2 冷凝液取样瓶
			var sampleBottleCondensate = new SampleBottle()
			{
				Function = FunctionType.Sample_Condensate,
				MaterialName = "乙醇冷凝液样品",
				CurrentVolume = 7,
				ExperimentId = Guid.NewGuid(),
				ExperimentName = "乙醇浓缩实验",
				ExperimentDescription = "乙醇浓缩实验的冷凝液取样",
				ExperimentStartTime = DateTime.Now.AddHours(-3),
				StackInTime = DateTime.Now.AddHours(-1),
				Operator = "张工",
				BarCode = "SP008-002"
			};

			// 3.3 旋蒸残液取样瓶
			var sampleBottleResidue = new SampleBottle()
			{
				Function = FunctionType.Sample_Residue,
				MaterialName = "丙酮残液样品",
				CurrentVolume = 5,
				ExperimentId = Guid.NewGuid(),
				ExperimentName = "丙酮浓缩实验",
				ExperimentDescription = "丙酮浓缩实验的残液取样",
				ExperimentStartTime = DateTime.Now.AddHours(-6),
				StackInTime = DateTime.Now.AddHours(-2),
				Operator = "李工",
				BarCode = "SP008-003"
			};


			//货架位置 101-107:可防止离心瓶、取样瓶
			for (int i = 1; i <= 7; i++)
			{
				locations.Add(new StorageLocation(
					LocationCode.Shelf,
					100 + i,
					new List<ItemType> {
						ItemType.CentrifugeBottle_500ml,
						ItemType.SampleBottle_8ml
					}
				));
			}

			//货架位置 201-204 可放置离心瓶、取样瓶
			for (int i = 1; i <= 4; i++)
			{
				locations.Add(new StorageLocation(
					LocationCode.Shelf,
					200 + i,
					new List<ItemType> {
						ItemType.CentrifugeBottle_500ml,
						ItemType.SampleBottle_8ml
					}
				));
			}

			//货架位置 205-206 只可放置取样瓶
			for (int i = 1; i <= 2; i++)
			{
				locations.Add(new StorageLocation(
					LocationCode.Shelf,
					204 + i,
					new List<ItemType> {
						ItemType.SampleBottle_8ml
					}
				));
			}

			//货架位置 301-304 可放置离心瓶、取样瓶
			for (int i = 1; i <= 4; i++)
			{
				locations.Add(new StorageLocation(
					LocationCode.Shelf,
					300 + i,
					new List<ItemType> {
						ItemType.CentrifugeBottle_500ml,
						ItemType.SampleBottle_8ml
					}
				));
			}

			//货架位置 305-306 都可放置
			for (int i = 1; i <= 2; i++)
			{
				locations.Add(new StorageLocation(
					LocationCode.Shelf,
					304 + i,
					new List<ItemType> {
						ItemType.Rotovap_500ml,
						ItemType.Rotovap_1000ml,
						ItemType.CentrifugeBottle_500ml,
						ItemType.SampleBottle_8ml
					}
				));
			}

			//货架位置 401-405 都可放置
			for (int i = 1; i <= 5; i++)
			{
				locations.Add(new StorageLocation(
					LocationCode.Shelf,
					400 + i,
					new List<ItemType> {
						ItemType.Rotovap_500ml,
						ItemType.Rotovap_1000ml,
						ItemType.CentrifugeBottle_500ml,
						ItemType.SampleBottle_8ml
					}
				));
			}

			//101 放置空离心瓶
			locations.Where(l => l.Position == 101).First().SetItem(centrifugeBottleEmpty);


			//102 放置洗液离心瓶
			locations.Where(l => l.Position == 102).First().SetItem(centrifugeBottleWash);


			//103 放置原液离心瓶
			locations.Where(l => l.Position == 103).First().SetItem(centrifugeBottleToRotovap);


			//104 放置冷凝液收集瓶
			locations.Where(l => l.Position == 104).First().SetItem(centrifugeBottleCondensate);

			//105 放置残液收集瓶
			locations.Where(l => l.Position == 105).First().SetItem(centrifugeBottleResidue);

			//201 放置空取样瓶
			locations.Where(l => l.Position == 201).First().SetItem(sampleBottleEmpty);

			//202 放置冷凝液取样瓶
			locations.Where(l => l.Position == 202).First().SetItem(sampleBottleCondensate);


			//203 放置残液取样瓶
			locations.Where(l => l.Position == 203).First().SetItem(sampleBottleResidue);


			//205 放置空取样瓶
			locations.Where(l => l.Position == 205).First().SetItem(sampleBottleEmpty);


			//305 放置1000ml旋蒸空瓶
			locations.Where(l => l.Position == 305).First().SetItem(rotovapBottle1000Empty);


			//306 放置1000ml旋蒸残液瓶
			locations.Where(l => l.Position == 306).First().SetItem(rotovapBottle1000Residue);


			//401 放置500ml旋蒸空瓶
			locations.Where(l => l.Position == 401).First().SetItem(rotovapBottle500Empty);


			//402 放置500ml旋蒸残液瓶
			locations.Where(l => l.Position == 402).First().SetItem(rotovapBottle500Residue);


			// 转移工作站 1, 2: 可放置所有类型瓶子
			for (int i = 1; i <= 2; i++)
			{
				locations.Add(new StorageLocation(
					LocationCode.TransferStation,
					i,
					new List<ItemType> {
						ItemType.Rotovap_500ml,
						ItemType.Rotovap_1000ml,
						ItemType.CentrifugeBottle_500ml,
						ItemType.SampleBottle_8ml
					}
				));
			}

			// 液体处理工作站 1: 可放置离心瓶和取样瓶
			locations.Add(new StorageLocation(
				LocationCode.LiquidHandlingStation,
				1,
				new List<ItemType> {
					ItemType.CentrifugeBottle_500ml,
					ItemType.SampleBottle_8ml
				}
			));

			// 液体处理工作站 2: 可放置所有瓶子
			locations.Add(new StorageLocation(
				LocationCode.LiquidHandlingStation,
				2,
				new List<ItemType> {
					ItemType.Rotovap_500ml,
					ItemType.Rotovap_1000ml,
					ItemType.CentrifugeBottle_500ml,
					ItemType.SampleBottle_8ml
				}
			));

			// 创建夹爪实例
			var normalGripper = new Gripper(ItemType.NormalGripper)
			{

			};

			var rotovapGripper = new Gripper(ItemType.RotovapGripper)
			{

			};

			// 夹爪货架位置
			locations.Add(new StorageLocation(
				LocationCode.GripperShelf,
				1,
				new List<ItemType> { ItemType.NormalGripper },
				normalGripper    // 添加普通夹爪
			));

			locations.Add(new StorageLocation(
				LocationCode.GripperShelf,
				2,
				new List<ItemType> { ItemType.RotovapGripper },
				rotovapGripper   // 添加旋蒸夹爪
			));

			await context.StorageLocations.AddRangeAsync(locations);
			await context.SaveChangesAsync();
		}
	}
}